package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.crm.domain.pojo.CrmEarlyWarningModel;
import com.yu.crm.domain.pojo.CrmEarlyWarningModelType;
import com.yu.crm.mapper.CrmEarlyWarningModelMapper;
import com.yu.crm.service.ICrmEarlyWarningModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzz.common.config.UserCacheManager;
import com.yxzz.common.helper.DataHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 预警模式表
 * 服务实现类
 * </p>
 *
 * @author pan
 * @since 2020-06-29
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmEarlyWarningModelService extends ServiceImpl<CrmEarlyWarningModelMapper, CrmEarlyWarningModel> implements ICrmEarlyWarningModelService {
    @Autowired
    CrmEarlyWarningModelTypeService earlyWarningModelTypeService;
    @Autowired
    CrmEarlyWarningTypeService crmEarlyWarningTypeService;
    @Autowired
    UserCacheManager userCacheManager;

    @Override
    public void saveOrUpdateModel(CrmEarlyWarningModel model) {
        if (DataHelpers.isNotEmpty(model.getId())) {
            model.setGmtModified(LocalDateTime.now());
        } else {
            model.setCreateUserId(userCacheManager.getCurrentUser().getUserId());
            model.setGmtCreate(LocalDateTime.now());
            model.setGmtModified(LocalDateTime.now());
        }
        super.saveOrUpdate(model);
        List<CrmEarlyWarningModelType> modelTypes = new ArrayList<>();
        List<Long> ids = model.getEarlyWarningTypeIds();
        for (Long id : ids) {
            CrmEarlyWarningModelType modelType = new CrmEarlyWarningModelType();
            modelType.setEarlyWarningModelId(model.getId());
            modelType.setEarlyWarningTypeId(id);
            modelType.setGmtCreate(LocalDateTime.now());
            modelType.setGmtModified(LocalDateTime.now());
            modelTypes.add(modelType);
        }
        if(modelTypes.size()>0){
            QueryWrapper<CrmEarlyWarningModelType> wrapper = new QueryWrapper<>();
            wrapper.eq("early_warning_model_id",model.getId());
            earlyWarningModelTypeService.remove(wrapper);
            earlyWarningModelTypeService.saveBatch(modelTypes);
        }
    }

    @Override
    public void deleteById(Long id) {
        QueryWrapper<CrmEarlyWarningModelType> wrapper = new QueryWrapper();
        wrapper.eq("early_warning_model_id", id);
        earlyWarningModelTypeService.remove(wrapper);
        this.removeById(id);
    }

    @Override
    public CrmEarlyWarningModel getInfoById(Long id) {
        return baseMapper.getInfoById(id);

    }


    @Override
    public IPage<CrmEarlyWarningModel> query(Page<CrmEarlyWarningModel> page, CrmEarlyWarningModel queryInfo) {
//        String userId = userCacheManager.getCurrentUser().getUserId();
        return baseMapper.query(page, queryInfo);

    }
}
